﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CharitiesLib;
using System.IO;
namespace Charities
{
    public partial class PictureAddForm : Form
    {
        int personId;
        string side;
        ModelContainer ctx = new ModelContainer();
        Form fo = null;
        public bool is_loaded = false;
        public PictureAddForm(int p, string str,Form m)
        {
            fo = m;
            this.MdiParent = m;
            personId = p;
            side = str;
            InitializeComponent();
        }
        private string trimstring(string str)
        {
            string temp = str.Replace(':', '_');
            temp = temp.Replace(' ', '_');
            return temp;
        }

        private void PictureAddForm_Load(object sender, EventArgs e)
        {
            try
            {
                if (side == "front")
                {
                    Person p = ctx.People.Single(c => c.Id == personId);
                    ReadtoPictureBox(p.PhotoFront);
                }
                else if (side == "back")
                {
                    Person p = ctx.People.Single(c => c.Id == personId);
                    ReadtoPictureBox(p.PhotoBack);
                }
            }
            catch (Exception)
            {


            }
            is_loaded = true;
        }
        private void ReadtoPictureBox(byte[] pic)
        {
            MemoryStream stream = new MemoryStream(pic);
            IdPhoto.Image = Image.FromStream(stream);
        }


        private void PictureAddForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            is_loaded = false;
            // remove this form from windows list
            ((Forms.FormMDI)fo).removeFormFromWindowsList(this);
        }

        private void PictureAddForm_Activated(object sender, EventArgs e)
        {
            // activate this form and add it windows list as activate one
            ((Forms.FormMDI)this.MdiParent).activateWindow(this);
        }
    }
}
